﻿// This code is released under the BSD license.
namespace PugLib.Net
{
    using System;
    using System.Net;

    public static class FindIP
    {
        public static string FirstIpv4NonLoopback
        {
            get
            {
                IPHostEntry ipEntry = Dns.GetHostEntry(Environment.MachineName);
                IPAddress[] addr = ipEntry.AddressList;

                foreach (IPAddress ip in addr)
                {
                    if (!IPAddress.IsLoopback(ip) &&
                        !ip.IsIPv6LinkLocal &&
                        !ip.IsIPv6Multicast &&
                        !ip.IsIPv6SiteLocal)
                    {
                        return ip.ToString();
                    }
                }
                return string.Empty;
            }
        }
    }
}